import numpy as np
from sklearn.mixture import GaussianMixture
import pandas
import pickle
import os
import p
import torch
import gc
datastr = r"D:/emotiondataset/"
import fitter
X=[[],[],[],[]]
Y=[]
tensortonumpy = lambda x: np.array(x.cpu().tolist())
b = pandas.read_csv(r"E:\dogbarkdata\AudioClassification\dataset\dogbarking\dataset_2.csv")
for index in range(1):#sum:400
    filestr = r"D:/old/Desktop/old/animal/barking-emotion-recognition/data/audioset_audios/" + b["ytid"][
        index] + "_" + str(int(b["start"][index])) + "_" + str(int(b["stop"][index])) + "_cut.mp3"
    if os.path.exists(filestr):
        X = fitter.fitterload(filestr)
        # x = np.array(x11+x12+[x13] + [x14])
        if b["label"][index] == 'Happy':
            y = 0
        elif b["label"][index] == 'Aggressive':
            y = 1
        else:
            y = 2
        # print(x,y)
        # print(len(x))
        Y.append(y)
        gc.collect()
#print(X)
for i in range(len(X)):
    X[i] = np.array(X[i]).reshape(-1,1)
    X[i] = np.nan_to_num(X[i])
Y = np.array(Y)
Y = np.nan_to_num(Y)
max_test = 0
gmmList=[]
for i in range(4):
    # 训练GMM模型
    gmm = GaussianMixture(n_components=3, random_state=20050421)
    gmm.fit(X[i])
    gmmList.append(gmm)
X=[[],[],[],[]]
Y=[]
for index in range(1):#401,604
    filestr = r"D:/old/Desktop/old/animal/barking-emotion-recognition/data/audioset_audios/" + b["ytid"][
        index] + "_" + str(int(b["start"][index])) + "_" + str(int(b["stop"][index])) + "_cut.mp3"
    if os.path.exists(filestr):
        X=fitter.fitterload(filestr)
        #x = np.array(x11+x12+[x13] + [x14])
        if b["label"][index] == 'Happy':
            y = 0
        elif b["label"][index] == 'Aggressive':
            y = 1
        else:
            y = 2
        #print(x,y)
        #print(len(x))
        Y.append(y)
        gc.collect()
#print(X)
for i in range(len(X)):
    X[i] = np.array(X[i]).reshape(-1,1)
    X[i] = np.nan_to_num(X[i])
Y = np.array(Y)
Y = np.nan_to_num(Y)
excel_file = 'output.xlsx'
ylist=[]
for i in range(4):
    ylist.append(gmmList[i].predict(X[i]))
ylist.append(Y)
df = pandas.DataFrame(ylist)
df.to_excel(excel_file, index=False)
